package cn.iocoder.yudao.framework.operatelog.api.service.permission.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.operatelog.api.controller.vo.ApiPermissionCreateReqVO;
import cn.iocoder.yudao.framework.operatelog.api.controller.vo.ApiPermissionExportReqVO;
import cn.iocoder.yudao.framework.operatelog.api.controller.vo.ApiPermissionPageReqVO;
import cn.iocoder.yudao.framework.operatelog.api.controller.vo.ApiPermissionUpdateReqVO;
import cn.iocoder.yudao.framework.operatelog.api.convert.permission.ApiPermissionConvert;
import cn.iocoder.yudao.framework.operatelog.api.dal.dataobject.permission.ApiPermissionDO;
import cn.iocoder.yudao.framework.operatelog.api.dal.mysql.permission.ApiPermissionMapper;
import cn.iocoder.yudao.framework.operatelog.api.enums.ApiErrorCodeConstants;
import cn.iocoder.yudao.framework.operatelog.api.service.permission.ApiPermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * Service 实现类
 *
 * @author 芋艿
 */
@Service
public class ApiPermissionServiceImpl implements ApiPermissionService {

    @Resource
    private ApiPermissionMapper permissionMapper;

    @Override
    public Integer createPermission(ApiPermissionCreateReqVO createReqVO) {
        // 插入
        ApiPermissionDO permission = ApiPermissionConvert.INSTANCE.convert(createReqVO);
        permission.setId(null);
        permissionMapper.insert(permission);
        // 返回
        return permission.getId();
    }

    @Override
    public void updatePermission(ApiPermissionUpdateReqVO updateReqVO) {
        // 校验存在
        this.validatePermissionExists(updateReqVO.getId());
        // 更新
        ApiPermissionDO updateObj = ApiPermissionConvert.INSTANCE.convert(updateReqVO);
        permissionMapper.updateById(updateObj);
    }

    @Override
    public void deletePermission(Integer id) {
        // 校验存在
        this.validatePermissionExists(id);
        // 删除
        permissionMapper.deleteById(id);
    }

    private void validatePermissionExists(Integer id) {
        if (permissionMapper.selectById(id) == null) {
            throw exception(ApiErrorCodeConstants.PERMISSION_NOT_EXISTS);
        }
    }

    @Override
    public ApiPermissionDO getPermission(Integer id) {
        return permissionMapper.selectById(id);
    }

    @Override
    public List<ApiPermissionDO> getPermissionList(Collection<Integer> ids) {
        return permissionMapper.selectBatchIds(ids);
    }

    @Override
    public List<ApiPermissionDO> getAllPermissionList() {
        return permissionMapper.selectList();
    }

    @Override
    public PageResult<ApiPermissionDO> getPermissionPage(ApiPermissionPageReqVO pageReqVO) {
        return permissionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ApiPermissionDO> getPermissionList(ApiPermissionExportReqVO exportReqVO) {
        return permissionMapper.selectList(exportReqVO);
    }

}
